﻿using System;
using System.ComponentModel.DataAnnotations;

namespace WebSiteFool
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
    public class FileExtensionsAttribute : ValidationAttribute
    {
        private string[] _allowedExtensions;
        public FileExtensionsAttribute(params string[] allowedExtensions)
        {
            _allowedExtensions = allowedExtensions;
        }
        public override bool IsValid(object value)
        {
            foreach(var ext in _allowedExtensions)
            {
                if(ext ==value.ToString() )
                {
                    return true;
                }
            }
            return false;
        }

        public override string FormatErrorMessage(string name)
        {
            return "Only the following file type supported: " + string.Join(",", _allowedExtensions);
        }
    }
}